﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;
using HMS.Common;
using HMS.Security;
using System.Threading;
using HMS.Logging;

namespace HMS.PL.Forms
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            
            try
            {

                string strQry = " SELECT M_USER.*,M_USER_TYPE.* FROM M_USER ";
                strQry = strQry + " INNER JOIN  M_USER_TYPE ON  M_USER_TYPE.USER_TYPE_ID= M_USER.USER_TYPE_ID";
                strQry = strQry + " WHERE LOGIN_NAME = '" + UserName.Text + "'";
                strQry = strQry + " AND PASSWORD = '" + Password.Text + "'";

                //string strQry = " SELECT * FROM X_HIERARCHIES ";
                Connection sqlConn = new Connection();

                DataSet ds = sqlConn.GetDataSetFromFactory(strQry);
                DataTable dt = ds.Tables[0];

                //FormsAuthentication.HashPasswordForStoringInConfigFile(

                if (dt.Rows.Count > 0)
                {
                    HMSIdentity userIdentity = new HMSIdentity(dt.Rows[0]["USER_ID"].ToString(), Convert.ToInt16(dt.Rows[0]["USER_ID"]), 
                                                               true, true, UserName.Text, "someuser@some.com", "", 
                                                               Convert.ToBoolean(dt.Rows[0]["IS_DASHBOARD_ALLOWED"]), 
                                                               Convert.ToBoolean(dt.Rows[0]["IS_DIMESIONS_ALLOWED"]), 
                                                               Convert.ToBoolean(dt.Rows[0]["IS_GROUPING_ALLOWED"]), 
                                                               Convert.ToBoolean(dt.Rows[0]["IS_REPORT_ALLOWED"]), 
                                                               Convert.ToBoolean(dt.Rows[0]["IS_CONFIG_ALLOWED"]));
                    //HMSPrincipal principal = new HMSPrincipal(userIdentity, roles);
                    HMSPrincipal principal = new HMSPrincipal(userIdentity);
                    Context.User = principal;
                    //string estr = CustomAuthentication.Encrypt(userIdentity);
                    HMSAuthentication.RedirectFromLoginPage(userIdentity);
                    
                    Session["LOGIN_NAME"] = UserName.Text;
                    Session["USER_ID"] = dt.Rows[0]["USER_ID"];
                    //Response.Redirect("../Forms/Welcome.aspx");
                    Response.Redirect("../Forms/Home.aspx");

                }
                else
                {
                   Msg.Text = "Invalid credentials. Please try again.";
                }

            }
            catch (Exception ex)
            {
                Util.WritErrorLog("$Login$", "LoginButton_Click", "Error in LoginButton_Click", "", ex);
                throw ex;
            } 
        }
       
    }
}